package com.zjhn.ds.utils.base;

import com.zjhn.ds.domain.entity.erp.Department;
import com.zjhn.ds.domain.entity.jlw.AssignTask;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @BelongsProject: ds_jlw
 * @BelongsPackage: com.zjhn.ds.utils
 * @Author: sxt
 * @CreateTime: 2023-09-11  09:42
 * @Description: 自定义工具类
 * @Version: 1.0
 */
@Slf4j
public class CustomUtil {

    /**
     * 获取卡号 解析8位卡号
     * @param code
     * @return
     */
    public static String getCardNo(String code){
        try {
            if (code.length() == 8){
                String str1 = code.substring(0, 2);
                String str2 = code.substring(2, 4);
                String str3 = code.substring(4, 6);
                String str4 = code.substring(6, 8);
                code = String.valueOf(Long.parseLong(str4 + str3 + str2 + str1, 16));
                code = formatWithMakingUp(code);
            }
        } catch (Exception e) {
            log.info("====[INFO]====CommonUtil====GetCardNo 解析卡号失败,e:{}",e.getMessage());
        }
        return code;
    }

    private static String formatWithMakingUp(String src) {
        if (null == src) {
            return null;
        }
        String FORMAT_STRING = "0000000000";
        int delta = FORMAT_STRING.length() - src.length();
        if (delta <= 0) {
            return src;
        }
        return FORMAT_STRING.substring(0, delta) + src;
    }


    /**
     * UUID和GUID的转换
     * @param uuid
     * @return
     */
    public static String uuid2GUID(String uuid){
        byte[] bytes = new byte[]{8,4,4,4,12};
        int start = 0;
        StringBuffer sb = new StringBuffer();
        for (byte aByte : bytes) {
            if (start != 0) {
                sb.append("-");
            }
            sb.append(uuid, start, start + aByte);
            start = start + aByte;
        }
        return sb.toString();
    }

    /**
     * 计算工位派工任务预计结束时间
     * @param assignTaskList
     * @return
     */
    public static Date calculateFinishTime(List<AssignTask> assignTaskList) {
        assignTaskList = assignTaskList.stream().sorted(Comparator.comparing(AssignTask::getBeginTime)).collect(Collectors.toList());
        AssignTask preTask = null;
        Date preFinishTime = null;
        for (AssignTask assignTask : assignTaskList) {
            if (preTask == null)
                preFinishTime = DateUtil.dateAddMinute(assignTask.getBeginTime(), assignTask.getRequire());
            else {
                if (preFinishTime.compareTo(assignTask.getBeginTime()) < 0)
                    // 结束时间小于下个任务的开始时间 以下个任务的开始时间进行计算新的结束时间
                    preFinishTime = DateUtil.dateAddMinute(assignTask.getBeginTime(), assignTask.getRequire());
                else
                    // 结束时间大于下个任务的开始时间 以上个任务的结束时间进行计算新的结束时间
                    preFinishTime = DateUtil.dateAddMinute(preFinishTime, assignTask.getRequire());
            }
            preTask = assignTask;
        }
        return preFinishTime;
    }

    /**
     * 获取部门 父节点及其所有子节点id
     * @param ids
     * @param parentId
     * @param list
     */
    public static void getDepartmentSon(List<String> ids, String parentId, List<Department> list) {
        for(Department department: list){
            //遍历出父id等于参数的id，add进子节点集合
            if(parentId.equals(department.getParentGuid())){
                //递归遍历下一级
                getDepartmentSon(ids,department.getId(),list);
                ids.add(department.getId());
            }
        }
    }

    /**
     * 十六进制字符串转为十进制数字
     *
     * @param hexString
     * @return
     */
    public static Integer hexStringToInt(String hexString) {
        hexString = hexString.replaceAll(" ", "");
        return Integer.parseInt(hexString, 16);
    }

    /**
     * 十六进制字符串转字节数组
     *
     * @param hexString
     * @return
     */
    public static byte[] hexStringToByteArray(String hexString) {
        hexString = hexString.replaceAll(" ", "");
        int len = hexString.length();
        byte[] bytes = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            // 两位一组，表示一个字节,把这样表示的16进制字符串，还原成一个字节
            bytes[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character
                    .digit(hexString.charAt(i + 1), 16));
        }
        return bytes;
    }

    /**
     * 字节数组转十六进制
     *
     * @param byteArray
     * @return
     */
    public static String byteArrayToHexStr(byte[] byteArray) {
        if (byteArray == null)
            return null;
        char[] hexArray = "0123456789ABCDEF".toCharArray();
        char[] hexChars = new char[byteArray.length * 2];
        for (int j = 0; j < byteArray.length; j++) {
            int v = byteArray[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }

}
